// goods-list.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cat_id:'',
    access_token:'',
    goods: [],
    search_word:'',
    sale:true,
    comment:true,
  },

  // 获取数据
  getData(sale, comment,keyword){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      self = this
    let data = {}
    data.cat_id = this.data.cat_id
    data.access_token = access_token
    sale && sale == 'asc' ? data.sale = 'asc' : (sale && sale == 'desc' ? data.sale = 'desc' :'' )
    comment && comment == 'asc' ? data.comment = 'asc' : (comment && comment == 'desc' ? data.comment = 'desc' : '')
    keyword ? data.name = keyword : ''
    wx.request({
      url: baseUrl + '/Index/product_list',
      method: 'get',
      data,
      success: data => {
        if(data.data.code == 200){
          console.log(data.data.data)
          self.setData({
            name: data.data.data,
            goods: data.data.data.list          
          })
          wx.hideLoading()
        }
      }
    })
  },
  input(e){
    this.setData({
      search_word: e.detail.value
    })
  },
  // 搜索
  search(){
    let word = this.data.search_word
    this.getData('','',word)
  },
  // 排序
  sort(e){
    let self = this
    if(e.target.dataset.name == 'sale'){
      self.data.sale ? self.getData('desc') : self.getData('asc')
      self.setData({
        sale: !self.data.sale
      })
    }else if(e.target.dataset.name == 'comment'){
      self.data.comment ? self.getData('','desc') : self.getData('','asc')
      self.setData({
        comment: !self.data.comment
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let cat_id = options.cat_id
    let isIndex = options.isIndex
    this.setData({ 
      cat_id, 
      isIndex,
      name: options.name  //options 为页面路由过程中传递的参数
    })
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    console.log('name:', this.data.name.category.cat_name);
    wx.setNavigationBarTitle({
      title: that.data.name.category.cat_name //页面标题为路由参数
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})